<template>
<div class="wrap">
  <swiper :options="swiperOption" v-if="showSwiper">
    <swiper-slide v-for="item in list" :key="item.id">
      <img class="swipe-img" :src="item.url" :alt="item.title">
    </swiper-slide>

    <!-- Optional controls -->
    <div class="swiper-pagination"  slot="pagination"></div>
    <!--<div class="swiper-button-prev" slot="button-prev"></div>
    <div class="swiper-button-next" slot="button-next"></div>
    <div class="swiper-scrollbar"   slot="scrollbar"></div>-->
  </swiper>
</div>
</template>

<script>
export default {
  name: 'HomeSwiper',
  props: {
    list: Array
  },
  data () {
    return {
      swiperOption: {
        pagination: '.swiper-pagination',
        loop: true
      }
    }
  },
  computed: {
    showSwiper () {
      // 该计算属性返回的是list的长度，也就是有list内容时才加载swiper组件，解决默认加载显示最后一张图片的BUG
      return this.list.length
    }
  }
}
</script>

<style lang="stylus" scoped>
/*因设置了scoped，所以直接修改样式无效，使用“>>>”做个穿透可实现效果*/
.wrap >>> .swiper-pagination-bullet-active
  background:#fff;
.wrap
  width:100%
  /****【小技巧】使用以下方法可以让该元素始终保持大小和图片的大小比例一致，防止因图片加载问题影响下面的布局****/
  /*padding-bottom的百分比值是内部图片的宽高比*/
  /*height的百分比值是相对父级元素的，而padding-bottom的百分比值是相对本元素的width的*/
  height:0
  padding-bottom:26.7%
  overflow:hidden
  .swipe-img
    width:100%
  .swiper-pagination
    bottom:0
</style>
